package dataManagement.action.manager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import dataImport.action.CustomActionReader;
import dataImport.action.EmoteBeforeActionReader;
import dataImport.action.PredefinedActionReader;
import dataImport.action.RequirementReader;
import dataImport.action.manager.EntityReaderManager;
import dataImport.exception.MissingReader;
import dataImport.model.CustomAction;
import dataImport.model.EmoteBeforeAction;
import dataImport.model.PredefinedAction;
import dataImport.model.Requirement;
import dataImport.model.abstracts.AbstractEntity;


public class ActionLibrary implements Serializable {

	private static final long serialVersionUID = -8421567342967568400L;

	private HashMap<String, List<Requirement>> requirements;

	private HashMap<String, List<PredefinedAction>> actions;

	private HashMap<String, EmoteBeforeAction> emoteBefore;

	public ActionLibrary(final EntityReaderManager entityReaderManager) throws MissingReader {
		this.requirements = new HashMap<String, List<Requirement>>();
		this.actions = new HashMap<String, List<PredefinedAction>>();
		this.emoteBefore = new HashMap<String, EmoteBeforeAction>();

		this.organizeRequirements(entityReaderManager);
		this.organizeActions(entityReaderManager);
		this.organizeEmotes(entityReaderManager);
	}

	private final void organizeRequirements(final EntityReaderManager entityReaderManager) throws MissingReader {
		final CustomActionReader customActionReader = entityReaderManager.getCustomActionReader();
		final RequirementReader requirementReader = entityReaderManager.getRequirementReader();

		for (AbstractEntity abstractEntity : customActionReader.getAbstractEntities()) {
			CustomAction customAction = (CustomAction) abstractEntity;
			this.requirements.put(customAction.getCommand().getPattern(), new ArrayList<Requirement>());
		}

		for (AbstractEntity abstractEntity : requirementReader.getAbstractEntities()) {
			Requirement requirement = (Requirement) abstractEntity;
			this.requirements.get(requirement.getCustomAction().getCommand().getPattern()).add(requirement);
		}
	}

	private final void organizeActions(final EntityReaderManager entityReaderManager) throws MissingReader {
		final CustomActionReader customActionReader = entityReaderManager.getCustomActionReader();
		final PredefinedActionReader predefinedActionReader = entityReaderManager.getPredefinedActionReader();

		for (AbstractEntity abstractEntity : customActionReader.getAbstractEntities()) {
			CustomAction customAction = (CustomAction) abstractEntity;
			this.actions.put(customAction.getCommand().getPattern(), new ArrayList<PredefinedAction>());
		}

		for (AbstractEntity abstractEntity : predefinedActionReader.getAbstractEntities()) {
			PredefinedAction predefinedAction = (PredefinedAction) abstractEntity;
			this.actions.get(predefinedAction.getCustomAction().getCommand().getPattern()).add(predefinedAction);
		}
	}

	private final void organizeEmotes(final EntityReaderManager entityReaderManager) throws MissingReader {
		final CustomActionReader customActionReader = entityReaderManager.getCustomActionReader();
		final EmoteBeforeActionReader emoteBeforeActionReader = entityReaderManager.getEmoteBeforeActionReader();

		for (AbstractEntity abstractEntity : customActionReader.getAbstractEntities()) {
			CustomAction customAction = (CustomAction) abstractEntity;
			this.emoteBefore.put(customAction.getCommand().getPattern(), null);
		}

		for (AbstractEntity abstractEntity : emoteBeforeActionReader.getAbstractEntities()) {
			EmoteBeforeAction emoteBeforeAction = (EmoteBeforeAction) abstractEntity;
			this.emoteBefore.put(emoteBeforeAction.getCustomAction().getCommand().getPattern(), emoteBeforeAction);
		}
	}

	protected EmoteBeforeAction getEmoteBefore(final String action) {
		return this.emoteBefore.get(action);
	}

	protected List<PredefinedAction> getPredefinedActions(final String action) {
		return this.actions.get(action);
	}

	protected List<Requirement> getRequirements(final String action) {
		return this.requirements.get(action);
	}
}
